﻿using Prism.Commands;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using XH.SmartParking.IService;
using XH.SmartParking.Base;
using XH.SmartParking.Entities;
using XH.SmartParking.Models;
using XH.SmartParking.Service;

namespace XH.SmartParking.ViewModels.Pages
{
    public class UserManagementViewModel : ViewModelBase
    {
        IUserService _userService;
        IRoleService _roleService;
        IDialogService _dialogService;
        public UserManagementViewModel(
            IRegionManager regionManager,
            IUserService userService,
            IRoleService roleService,
            IDialogService dialogService)
            : base(regionManager)
        {
            PageTitle = "系统用户管理";

            _userService = userService;
            _roleService = roleService;
            _dialogService = dialogService;

            SelectRoleCommand = new DelegateCommand<UserModel>(DoSelectRole);
            ResetPasswordCommand = new DelegateCommand<object>(DoResetPassword);
            LockUserCommand = new DelegateCommand<object>(DoLockUser);

            Refresh();
        }

        public ObservableCollection<UserModelEx> Users { get; set; } =
            new ObservableCollection<UserModelEx>();

        public DelegateCommand<UserModel> SelectRoleCommand { get; set; }
        public DelegateCommand<object> ResetPasswordCommand { get; set; }
        public DelegateCommand<object> LockUserCommand { get; set; }

        public override void Refresh()
        {
            Users.Clear();
            var users = _userService.GetUsers(SearchKey).ToList();
            // 数据里拿到的是角色的ID   没有角色名称
            // 界面一般都会显示名称

            int index = 1;
            foreach (var user in users)
            {
                UserModelEx model = new UserModelEx
                {
                    Index = index++,
                    UserId = user.UserId,
                    UserName = user.UserName,
                    RealName = user.RealName,
                    UserIcon = "pack://siteoforigin:,,,/Avatarts/" + user.UserIcon,
                    Address = user.Address,
                    Age = user.Age,
                    Password = user.Password,
                    Gender = user.Gender,
                    Email = user.Email,
                    Status = user.Status,

                    LockButtonText = user.Status == 1 ? "锁定" : "启用"
                };
                var rs = _roleService.Query<SysRole>(r =>
                            user.Roles.Select(ur => ur.RoleId).ToList()
                            .Contains(r.RoleId)).ToList();
                model.Roles = rs.Select(r => new RoleModel
                {
                    RoleId = r.RoleId,
                    RoleName = r.RoleName,
                }).ToList();

                Users.Add(model);
            }
        }

        public override void DoDelete(object model)
        {
            try
            {
                if (MessageBox.Show("是否确定删除此项？", "提示", MessageBoxButton.YesNo) ==
                    MessageBoxResult.Yes)
                {
                    // 物理删除
                    _userService.Delete<SysUser>((model as UserModelEx).UserId);

                    MessageBox.Show("删除完成！", "提示");

                    Users.Remove(model as UserModelEx);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示");
            }
        }

        public override void DoModify(object model)
        {
            DialogParameters ps = new DialogParameters();
            ps.Add("model", model);
            _dialogService.ShowDialog("ModifyUserView", ps, result =>
            {
                // 判断子窗口的返回状态，如果OK，刷新当前页面，否则不管
                if (result.Result == ButtonResult.OK)
                {
                    Refresh();
                }
            });
        }

        public void DoResetPassword(object model)
        {
            try
            {
                if (MessageBox.Show("是否确定重置当前用户密码？", "提示", MessageBoxButton.YesNo) ==
                    MessageBoxResult.Yes)
                {
                    var user = _userService.Find<SysUser>((model as UserModel).UserId);
                    user.Password = "123456";
                    _userService.Update<SysUser>(user);

                    MessageBox.Show("重置完成！", "提示");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示");
            }
        }

        public void DoLockUser(object model)
        {
            try
            {
                var ui = model as UserModelEx;
                var user = _userService.Find<SysUser>(ui.UserId);
                if (ui.Status == 1)
                {
                    if (MessageBox.Show("是否锁定前用户？", "提示", MessageBoxButton.YesNo) ==
                        MessageBoxResult.Yes)
                    {
                        user.Status = 0;
                    }
                }
                else
                {
                    user.Status = 1;
                }
                _userService.Update<SysUser>(user);

                MessageBox.Show("操作已完成！", "提示");

                ui.Status = user.Status;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示");
            }
        }

        private void DoSelectRole(UserModel model)
        {
            DialogParameters ps = new DialogParameters();
            ps.Add("model", model);
            _dialogService.ShowDialog("SelectRoleView", ps, result =>
            {
                // 判断子窗口的返回状态，如果OK，刷新当前页面，否则不管
                if (result.Result == ButtonResult.OK)
                {
                    Refresh();
                }
            });
        }
    }
}
